<?php
spl_autoload_register(array('PhormEnvironment', 'autoload'));

/* SYSTEM ENVIRONMENT */
PhormEnvironment::requireFile('/phorm/application/PhormConfig.php');
//PhormEnvironment::requireFile('/phorm/application/PhormSession.php');
PhormEnvironment::requireFile('/phorm/application/PhormFunctions.php');
PhormEnvironment::requireFile('/phorm/application/PhormConstants.php');

/* DATABASE LAYER */
//PhormEnvironment::requireFile('/phorm/database/core/Query.php');
//PhormEnvironment::requireFile('/phorm/records/MySQLRecord.php');
//PhormEnvironment::requireFile('/phorm/database/mysql/MySQLQuery.php');
PhormEnvironment::requireFile('/phorm/records/CouchDBRecord.php');
PhormEnvironment::requireFile('/phorm/database/couchdb/CouchDB.php');

/* APPLICATION LAYER */
PhormEnvironment::requireFile('/application/models/Models.php');



class PhormEnvironment
{
	public static $classes = array();
	
	public static function requireFile($filename)
	{
		require_once $_SERVER["DOCUMENT_ROOT"].$filename;
	}
	
	public static function registerClass($name, $source)
	{
		self::$classes[$name] = $source;
	}
	
	public static function autoload($class)
	{
		if(PhormEnvironment::$classes[$class])
		{
			require_once($_SERVER["DOCUMENT_ROOT"].PhormEnvironment::$classes[$class]);
		}
		else
		{
			if(file_exists($_SERVER['DOCUMENT_ROOT'].'/phorm/model/fields/'. $class . '.php'))
				require_once $_SERVER['DOCUMENT_ROOT'].'/phorm/model/fields/'. $class . '.php';
		}
	}
}



// due to object serialization, unserialization class declaration requirements this needs to happen last:
//if (session_id() == "") session_start();

?>